package com.myz.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

// 延迟队列
@Configuration
public class DelayedQueueConfig {
    // 交换机
    public static final String DELAYED_EXCHANGE = "delayed.exchange";
    // 延迟队列
    public static final String DELAYED_QUEUE = "delayed.queue";
    // routingKey
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    // 声明交换机
    @Bean
    public CustomExchange delayedExchange(){
        // 自定义交换机 参数: 交换机名称 交换机类型 是否需要持久化 是否自动删除 其他参数
        Map<String,Object> arguments = new HashMap<>();
        // 其他参数 的map中放 延迟类型属性 延迟类型为直接类型
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(DELAYED_EXCHANGE,"x-delayed-message",true,false,arguments);
    }
    // 声明队列
    @Bean
    public Queue delayedQueue(){
        return new Queue(DELAYED_QUEUE);
    }
    // to binding queue and exchange
    @Bean
    public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue deplyedQueue,
                                        @Qualifier("delayedExchange") CustomExchange delayedExchange){
        return BindingBuilder.bind(deplyedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }

}
